home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / firefox-3.5.5 / components / nsBrowserContentHandler.js < prev    next >
Text File  |  2009-11-09  |  32KB  |  885 lines

  1. //@line 37 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  2.  
  3. const nsISupports            = Components.interfaces.nsISupports;
  4.  
  5. const nsIBrowserDOMWindow    = Components.interfaces.nsIBrowserDOMWindow;
  6. const nsIBrowserHandler      = Components.interfaces.nsIBrowserHandler;
  7. const nsIBrowserHistory      = Components.interfaces.nsIBrowserHistory;
  8. const nsIChannel             = Components.interfaces.nsIChannel;
  9. const nsICommandLine         = Components.interfaces.nsICommandLine;
  10. const nsICommandLineHandler  = Components.interfaces.nsICommandLineHandler;
  11. const nsIContentHandler      = Components.interfaces.nsIContentHandler;
  12. const nsIDocShellTreeItem    = Components.interfaces.nsIDocShellTreeItem;
  13. const nsIDOMChromeWindow     = Components.interfaces.nsIDOMChromeWindow;
  14. const nsIDOMWindow           = Components.interfaces.nsIDOMWindow;
  15. const nsIFactory             = Components.interfaces.nsIFactory;
  16. const nsIFileURL             = Components.interfaces.nsIFileURL;
  17. const nsIHttpProtocolHandler = Components.interfaces.nsIHttpProtocolHandler;
  18. const nsIInterfaceRequestor  = Components.interfaces.nsIInterfaceRequestor;
  19. const nsINetUtil             = Components.interfaces.nsINetUtil;
  20. const nsIPrefBranch          = Components.interfaces.nsIPrefBranch;
  21. const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
  22. const nsISupportsString      = Components.interfaces.nsISupportsString;
  23. const nsIURIFixup            = Components.interfaces.nsIURIFixup;
  24. const nsIWebNavigation       = Components.interfaces.nsIWebNavigation;
  25. const nsIWindowMediator      = Components.interfaces.nsIWindowMediator;
  26. const nsIWindowWatcher       = Components.interfaces.nsIWindowWatcher;
  27. const nsICategoryManager     = Components.interfaces.nsICategoryManager;
  28. const nsIWebNavigationInfo   = Components.interfaces.nsIWebNavigationInfo;
  29. const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
  30. const nsICommandLineValidator = Components.interfaces.nsICommandLineValidator;
  31.  
  32. const NS_BINDING_ABORTED = Components.results.NS_BINDING_ABORTED;
  33. const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
  34. const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
  35.  
  36. const URI_INHERITS_SECURITY_CONTEXT = nsIHttpProtocolHandler
  37.                                         .URI_INHERITS_SECURITY_CONTEXT;
  38.  
  39. function shouldLoadURI(aURI) {
  40.   if (aURI && !aURI.schemeIs("chrome"))
  41.     return true;
  42.  
  43.   dump("*** Preventing external load of chrome: URI into browser window\n");
  44.   dump("    Use -chrome <uri> instead\n");
  45.   return false;
  46. }
  47.  
  48. function resolveURIInternal(aCmdLine, aArgument) {
  49.   var uri = aCmdLine.resolveURI(aArgument);
  50.  
  51.   if (!(uri instanceof nsIFileURL)) {
  52.     return uri;
  53.   }
  54.  
  55.   try {
  56.     if (uri.file.exists())
  57.       return uri;
  58.   }
  59.   catch (e) {
  60.     Components.utils.reportError(e);
  61.   }
  62.  
  63.   // We have interpreted the argument as a relative file URI, but the file
  64.   // doesn't exist. Try URI fixup heuristics: see bug 290782.
  65.  
  66.   try {
  67.     var urifixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
  68.                              .getService(nsIURIFixup);
  69.  
  70.     uri = urifixup.createFixupURI(aArgument, 0);
  71.   }
  72.   catch (e) {
  73.     Components.utils.reportError(e);
  74.   }
  75.  
  76.   return uri;
  77. }
  78.  
  79. const OVERRIDE_NONE        = 0;
  80. const OVERRIDE_NEW_PROFILE = 1;
  81. const OVERRIDE_NEW_MSTONE  = 2;
  82. /**
  83.  * Determines whether a home page override is needed.
  84.  * Returns:
  85.  *  OVERRIDE_NEW_PROFILE if this is the first run with a new profile.
  86.  *  OVERRIDE_NEW_MSTONE if this is the first run with a build with a different
  87.  *                      Gecko milestone (i.e. right after an upgrade).
  88.  *  OVERRIDE_NONE otherwise.
  89.  */
  90. function needHomepageOverride(prefb) {
  91.   var savedmstone = null;
  92.   try {
  93.     savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone");
  94.   } catch (e) {}
  95.  
  96.   if (savedmstone == "ignore")
  97.     return OVERRIDE_NONE;
  98.  
  99.   var mstone = Components.classes["@mozilla.org/network/protocol;1?name=http"]
  100.                          .getService(nsIHttpProtocolHandler).misc;
  101.  
  102.   if (mstone != savedmstone) {
  103.     // Bug 462254. Previous releases had a default pref to suppress the EULA
  104.     // agreement if the platform's installer had already shown one. Now with
  105.     // about:rights we've removed the EULA stuff and default pref, but we need
  106.     // a way to make existing profiles retain the default that we removed.
  107.     if (savedmstone)
  108.       prefb.setBoolPref("browser.rights.3.shown", true);
  109.     
  110.     prefb.setCharPref("browser.startup.homepage_override.mstone", mstone);
  111.     return (savedmstone ? OVERRIDE_NEW_MSTONE : OVERRIDE_NEW_PROFILE);
  112.   }
  113.  
  114.   return OVERRIDE_NONE;
  115. }
  116.  
  117. // Copies a pref override file into the user's profile pref-override folder,
  118. // and then tells the pref service to reload its default prefs.
  119. function copyPrefOverride() {
  120.   try {
  121.     var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
  122.                                 .getService(Components.interfaces.nsIProperties);
  123.     const NS_APP_EXISTING_PREF_OVERRIDE = "ExistingPrefOverride";
  124.     var prefOverride = fileLocator.get(NS_APP_EXISTING_PREF_OVERRIDE,
  125.                                        Components.interfaces.nsIFile);
  126.     if (!prefOverride.exists())
  127.       return; // nothing to do
  128.  
  129.     const NS_APP_PREFS_OVERRIDE_DIR     = "PrefDOverride";
  130.     var prefOverridesDir = fileLocator.get(NS_APP_PREFS_OVERRIDE_DIR,
  131.                                            Components.interfaces.nsIFile);
  132.  
  133.     // Check for any existing pref overrides, and remove them if present
  134.     var existingPrefOverridesFile = prefOverridesDir.clone();
  135.     existingPrefOverridesFile.append(prefOverride.leafName);
  136.     if (existingPrefOverridesFile.exists())
  137.       existingPrefOverridesFile.remove(false);
  138.  
  139.     prefOverride.copyTo(prefOverridesDir, null);
  140.  
  141.     // Now that we've installed the new-profile pref override file,
  142.     // re-read the default prefs.
  143.     var prefSvcObs = Components.classes["@mozilla.org/preferences-service;1"]
  144.                                .getService(Components.interfaces.nsIObserver);
  145.     prefSvcObs.observe(null, "reload-default-prefs", null);
  146.   } catch (ex) {
  147.     Components.utils.reportError(ex);
  148.   }
  149. }
  150.  
  151. // Flag used to indicate that the arguments to openWindow can be passed directly.
  152. const NO_EXTERNAL_URIS = 1;
  153.  
  154. function openWindow(parent, url, target, features, args, noExternalArgs) {
  155.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  156.                          .getService(nsIWindowWatcher);
  157.  
  158.   if (noExternalArgs == NO_EXTERNAL_URIS) {
  159.     // Just pass in the defaultArgs directly
  160.     var argstring;
  161.     if (args) {
  162.       argstring = Components.classes["@mozilla.org/supports-string;1"]
  163.                             .createInstance(nsISupportsString);
  164.       argstring.data = args;
  165.     }
  166.  
  167.     return wwatch.openWindow(parent, url, target, features, argstring);
  168.   }
  169.   
  170.   // Pass an array to avoid the browser "|"-splitting behavior.
  171.   var argArray = Components.classes["@mozilla.org/supports-array;1"]
  172.                     .createInstance(Components.interfaces.nsISupportsArray);
  173.  
  174.   // add args to the arguments array
  175.   var stringArgs = null;
  176.   if (args instanceof Array) // array
  177.     stringArgs = args;
  178.   else if (args) // string
  179.     stringArgs = [args];
  180.  
  181.   if (stringArgs) {
  182.     // put the URIs into argArray
  183.     var uriArray = Components.classes["@mozilla.org/supports-array;1"]
  184.                        .createInstance(Components.interfaces.nsISupportsArray);
  185.     stringArgs.forEach(function (uri) {
  186.       var sstring = Components.classes["@mozilla.org/supports-string;1"]
  187.                               .createInstance(nsISupportsString);
  188.       sstring.data = uri;
  189.       uriArray.AppendElement(sstring);
  190.     });
  191.     argArray.AppendElement(uriArray);
  192.   } else {
  193.     argArray.AppendElement(null);
  194.   }
  195.  
  196.   // Pass these as null to ensure that we always trigger the "single URL"
  197.   // behavior in browser.js's BrowserStartup (which handles the window
  198.   // arguments)
  199.   argArray.AppendElement(null); // charset
  200.   argArray.AppendElement(null); // referer
  201.   argArray.AppendElement(null); // postData
  202.   argArray.AppendElement(null); // allowThirdPartyFixup
  203.  
  204.   return wwatch.openWindow(parent, url, target, features, argArray);
  205. }
  206.  
  207. function openPreferences() {
  208.   var features = "chrome,titlebar,toolbar,centerscreen,dialog=no";
  209.   var url = "chrome://browser/content/preferences/preferences.xul";
  210.  
  211.   var win = getMostRecentWindow("Browser:Preferences");
  212.   if (win) {
  213.     win.focus();
  214.   } else {
  215.     openWindow(null, url, "_blank", features);
  216.   }
  217. }
  218.  
  219. function getMostRecentWindow(aType) {
  220.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  221.                      .getService(nsIWindowMediator);
  222.   return wm.getMostRecentWindow(aType);
  223. }
  224.  
  225. // this returns the most recent non-popup browser window
  226. function getMostRecentBrowserWindow() {
  227.   var browserGlue = Components.classes["@mozilla.org/browser/browserglue;1"]
  228.                               .getService(Components.interfaces.nsIBrowserGlue);
  229.   return browserGlue.getMostRecentBrowserWindow();
  230. }
  231.  
  232. function doSearch(searchTerm, cmdLine) {
  233.   var ss = Components.classes["@mozilla.org/browser/search-service;1"]
  234.                      .getService(nsIBrowserSearchService);
  235.  
  236.   var submission = ss.defaultEngine.getSubmission(searchTerm, null);
  237.  
  238.   // fill our nsISupportsArray with uri-as-wstring, null, null, postData
  239.   var sa = Components.classes["@mozilla.org/supports-array;1"]
  240.                      .createInstance(Components.interfaces.nsISupportsArray);
  241.  
  242.   var wuri = Components.classes["@mozilla.org/supports-string;1"]
  243.                        .createInstance(Components.interfaces.nsISupportsString);
  244.   wuri.data = submission.uri.spec;
  245.  
  246.   sa.AppendElement(wuri);
  247.   sa.AppendElement(null);
  248.   sa.AppendElement(null);
  249.   sa.AppendElement(submission.postData);
  250.  
  251.   // XXXbsmedberg: use handURIToExistingBrowser to obey tabbed-browsing
  252.   // preferences, but need nsIBrowserDOMWindow extensions
  253.  
  254.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  255.                          .getService(nsIWindowWatcher);
  256.  
  257.   return wwatch.openWindow(null, nsBrowserContentHandler.chromeURL,
  258.                            "_blank",
  259.                            "chrome,dialog=no,all" +
  260.                              nsBrowserContentHandler.getFeatures(cmdLine),
  261.                            sa);
  262. }
  263.  
  264. var nsBrowserContentHandler = {
  265.   /* helper functions */
  266.  
  267.   mChromeURL : null,
  268.  
  269.   get chromeURL() {
  270.     if (this.mChromeURL) {
  271.       return this.mChromeURL;
  272.     }
  273.  
  274.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  275.                           .getService(nsIPrefBranch);
  276.     this.mChromeURL = prefb.getCharPref("browser.chromeURL");
  277.  
  278.     return this.mChromeURL;
  279.   },
  280.  
  281.   /* nsISupports */
  282.   QueryInterface : function bch_QI(iid) {
  283.     if (!iid.equals(nsISupports) &&
  284.         !iid.equals(nsICommandLineHandler) &&
  285.         !iid.equals(nsIBrowserHandler) &&
  286.         !iid.equals(nsIContentHandler) &&
  287.         !iid.equals(nsICommandLineValidator) &&
  288.         !iid.equals(nsIFactory))
  289.       throw Components.results.NS_ERROR_NO_INTERFACE;
  290.  
  291.     return this;
  292.   },
  293.  
  294.   /* nsICommandLineHandler */
  295.   handle : function bch_handle(cmdLine) {
  296.     if (cmdLine.handleFlag("browser", false)) {
  297.       // Passing defaultArgs, so use NO_EXTERNAL_URIS
  298.       openWindow(null, this.chromeURL, "_blank",
  299.                  "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  300.                  this.defaultArgs, NO_EXTERNAL_URIS);
  301.       cmdLine.preventDefault = true;
  302.     }
  303.  
  304.     try {
  305.       var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
  306.     }
  307.     catch (e) {
  308.       throw NS_ERROR_ABORT;
  309.     }
  310.  
  311.     if (remoteCommand != null) {
  312.       try {
  313.         var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
  314.         var remoteVerb;
  315.         if (a) {
  316.           remoteVerb = a[1].toLowerCase();
  317.           var remoteParams = [];
  318.           var sepIndex = a[2].lastIndexOf(",");
  319.           if (sepIndex == -1)
  320.             remoteParams[0] = a[2];
  321.           else {
  322.             remoteParams[0] = a[2].substring(0, sepIndex);
  323.             remoteParams[1] = a[2].substring(sepIndex + 1);
  324.           }
  325.         }
  326.  
  327.         switch (remoteVerb) {
  328.         case "openurl":
  329.         case "openfile":
  330.           // openURL(<url>)
  331.           // openURL(<url>,new-window)
  332.           // openURL(<url>,new-tab)
  333.  
  334.           // First param is the URL, second param (if present) is the "target"
  335.           // (tab, window)
  336.           var url = remoteParams[0];
  337.           var target = nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW;
  338.           if (remoteParams[1]) {
  339.             var targetParam = remoteParams[1].toLowerCase()
  340.                                              .replace(/^\s*|\s*$/g, "");
  341.             if (targetParam == "new-tab")
  342.               target = nsIBrowserDOMWindow.OPEN_NEWTAB;
  343.             else if (targetParam == "new-window")
  344.               target = nsIBrowserDOMWindow.OPEN_NEWWINDOW;
  345.             else {
  346.               // The "target" param isn't one of our supported values, so
  347.               // assume it's part of a URL that contains commas.
  348.               url += "," + remoteParams[1];
  349.             }
  350.           }
  351.  
  352.           var uri = resolveURIInternal(cmdLine, url);
  353.           handURIToExistingBrowser(uri, target, cmdLine);
  354.           break;
  355.  
  356.         case "xfedocommand":
  357.           // xfeDoCommand(openBrowser)
  358.           if (remoteParams[0].toLowerCase() != "openbrowser")
  359.             throw NS_ERROR_ABORT;
  360.  
  361.           // Passing defaultArgs, so use NO_EXTERNAL_URIS
  362.           openWindow(null, this.chromeURL, "_blank",
  363.                      "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  364.                      this.defaultArgs, NO_EXTERNAL_URIS);
  365.           break;
  366.  
  367.         default:
  368.           // Somebody sent us a remote command we don't know how to process:
  369.           // just abort.
  370.           throw "Unknown remote command.";
  371.         }
  372.  
  373.         cmdLine.preventDefault = true;
  374.       }
  375.       catch (e) {
  376.         Components.utils.reportError(e);
  377.         // If we had a -remote flag but failed to process it, throw
  378.         // NS_ERROR_ABORT so that the xremote code knows to return a failure
  379.         // back to the handling code.
  380.         throw NS_ERROR_ABORT;
  381.       }
  382.     }
  383.  
  384.     var uriparam;
  385.     try {
  386.       while ((uriparam = cmdLine.handleFlagWithParam("new-window", false))) {
  387.         var uri = resolveURIInternal(cmdLine, uriparam);
  388.         if (!shouldLoadURI(uri))
  389.           continue;
  390.         openWindow(null, this.chromeURL, "_blank",
  391.                    "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  392.                    uri.spec);
  393.         cmdLine.preventDefault = true;
  394.       }
  395.     }
  396.     catch (e) {
  397.       Components.utils.reportError(e);
  398.     }
  399.  
  400.     try {
  401.       while ((uriparam = cmdLine.handleFlagWithParam("new-tab", false))) {
  402.         var uri = resolveURIInternal(cmdLine, uriparam);
  403.         handURIToExistingBrowser(uri, nsIBrowserDOMWindow.OPEN_NEWTAB, cmdLine);
  404.         cmdLine.preventDefault = true;
  405.       }
  406.     }
  407.     catch (e) {
  408.       Components.utils.reportError(e);
  409.     }
  410.  
  411.     var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
  412.     if (chromeParam) {
  413.  
  414.       // Handle the old preference dialog URL separately (bug 285416)
  415.       if (chromeParam == "chrome://browser/content/pref/pref.xul") {
  416.         openPreferences();
  417.         cmdLine.preventDefault = true;
  418.       } else try {
  419.         // only load URIs which do not inherit chrome privs
  420.         var features = "chrome,dialog=no,all" + this.getFeatures(cmdLine);
  421.         var uri = resolveURIInternal(cmdLine, chromeParam);
  422.         var netutil = Components.classes["@mozilla.org/network/util;1"]
  423.                                 .getService(nsINetUtil);
  424.         if (!netutil.URIChainHasFlags(uri, URI_INHERITS_SECURITY_CONTEXT)) {
  425.           openWindow(null, uri.spec, "_blank", features);
  426.           cmdLine.preventDefault = true;
  427.         }
  428.       }
  429.       catch (e) {
  430.         Components.utils.reportError(e);
  431.       }
  432.     }
  433.     if (cmdLine.handleFlag("preferences", false)) {
  434.       openPreferences();
  435.       cmdLine.preventDefault = true;
  436.     }
  437.     if (cmdLine.handleFlag("silent", false))
  438.       cmdLine.preventDefault = true;
  439.  
  440.     var searchParam = cmdLine.handleFlagWithParam("search", false);
  441.     if (searchParam) {
  442.       doSearch(searchParam, cmdLine);
  443.       cmdLine.preventDefault = true;
  444.     }
  445.  
  446.     var fileParam = cmdLine.handleFlagWithParam("file", false);
  447.     if (fileParam) {
  448.       var file = cmdLine.resolveFile(fileParam);
  449.       var ios = Components.classes["@mozilla.org/network/io-service;1"]
  450.                           .getService(Components.interfaces.nsIIOService);
  451.       var uri = ios.newFileURI(file);
  452.       openWindow(null, this.chromeURL, "_blank", 
  453.                  "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  454.                  uri.spec);
  455.       cmdLine.preventDefault = true;
  456.     }
  457.  
  458. //@line 506 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  459.   },
  460.  
  461.   helpInfo : "  -browser            Open a browser window.\n",
  462.  
  463.   /* nsIBrowserHandler */
  464.  
  465.   get defaultArgs() {
  466.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  467.                           .getService(nsIPrefBranch);
  468.     var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"]
  469.                               .getService(Components.interfaces.nsIURLFormatter);
  470.  
  471.     var overridePage = "";
  472.     var haveUpdateSession = false;
  473.     try {
  474.       switch (needHomepageOverride(prefb)) {
  475.         case OVERRIDE_NEW_PROFILE:
  476.           // New profile
  477.           overridePage = formatter.formatURLPref("startup.homepage_welcome_url");
  478.           break;
  479.         case OVERRIDE_NEW_MSTONE:
  480.           // Existing profile, new build
  481.           copyPrefOverride();
  482.  
  483.           // Check whether we have a session to restore. If we do, we assume
  484.           // that this is an "update" session.
  485.           var ss = Components.classes["@mozilla.org/browser/sessionstartup;1"]
  486.                              .getService(Components.interfaces.nsISessionStartup);
  487.           haveUpdateSession = ss.doRestore();
  488.           overridePage = formatter.formatURLPref("startup.homepage_override_url");
  489.           break;
  490.     }
  491.     } catch (ex) {}
  492.  
  493.     // formatURLPref might return "about:blank" if getting the pref fails
  494.     if (overridePage == "about:blank")
  495.       overridePage = "";
  496.  
  497.     var startPage = "";
  498.     try {
  499.       var choice = prefb.getIntPref("browser.startup.page");
  500.       if (choice == 1 || choice == 3)
  501.         startPage = this.startPage;
  502.  
  503.       if (choice == 2)
  504.         startPage = Components.classes["@mozilla.org/browser/global-history;2"]
  505.                               .getService(nsIBrowserHistory).lastPageVisited;
  506.     } catch (e) {
  507.       Components.utils.reportError(e);
  508.     }
  509.  
  510.     if (startPage == "about:blank")
  511.       startPage = "";
  512.  
  513.     // Only show the startPage if we're not restoring an update session.
  514.     if (overridePage && startPage && !haveUpdateSession)
  515.       return overridePage + "|" + startPage;
  516.  
  517.     return overridePage || startPage || "about:blank";
  518.   },
  519.  
  520.   get startPage() {
  521.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  522.                           .getService(nsIPrefBranch);
  523.  
  524.     var uri = prefb.getComplexValue("browser.startup.homepage",
  525.                                     nsIPrefLocalizedString).data;
  526.  
  527.     if (!uri) {
  528.       prefb.clearUserPref("browser.startup.homepage");
  529.       uri = prefb.getComplexValue("browser.startup.homepage",
  530.                                   nsIPrefLocalizedString).data;
  531.     }
  532.                                 
  533.     var count;
  534.     try {
  535.       count = prefb.getIntPref("browser.startup.homepage.count");
  536.     }
  537.     catch (e) {
  538.       return uri;
  539.     }
  540.  
  541.     for (var i = 1; i < count; ++i) {
  542.       try {
  543.         var page = prefb.getComplexValue("browser.startup.homepage." + i,
  544.                                          nsIPrefLocalizedString).data;
  545.         uri += "\n" + page;
  546.       }
  547.       catch (e) {
  548.       }
  549.     }
  550.  
  551.     return uri;
  552.   },
  553.  
  554.   mFeatures : null,
  555.  
  556.   getFeatures : function bch_features(cmdLine) {
  557.     if (this.mFeatures === null) {
  558.       this.mFeatures = "";
  559.  
  560.       try {
  561.         var width = cmdLine.handleFlagWithParam("width", false);
  562.         var height = cmdLine.handleFlagWithParam("height", false);
  563.  
  564.         if (width)
  565.           this.mFeatures += ",width=" + width;
  566.         if (height)
  567.           this.mFeatures += ",height=" + height;
  568.       }
  569.       catch (e) {
  570.       }
  571.     }
  572.  
  573.     return this.mFeatures;
  574.   },
  575.  
  576.   /* nsIContentHandler */
  577.  
  578.   handleContent : function bch_handleContent(contentType, context, request) {
  579.     try {
  580.       var webNavInfo = Components.classes["@mozilla.org/webnavigation-info;1"]
  581.                                  .getService(nsIWebNavigationInfo);
  582.       if (!webNavInfo.isTypeSupported(contentType, null)) {
  583.         throw NS_ERROR_WONT_HANDLE_CONTENT;
  584.       }
  585.     } catch (e) {
  586.       throw NS_ERROR_WONT_HANDLE_CONTENT;
  587.     }
  588.  
  589.     request.QueryInterface(nsIChannel);
  590.     handURIToExistingBrowser(request.URI,
  591.       nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, null);
  592.     request.cancel(NS_BINDING_ABORTED);
  593.   },
  594.  
  595.   /* nsICommandLineValidator */
  596.   validate : function bch_validate(cmdLine) {
  597.     // Other handlers may use osint so only handle the osint flag if the url
  598.     // flag is also present and the command line is valid.
  599.     var osintFlagIdx = cmdLine.findFlag("osint", false);
  600.     var urlFlagIdx = cmdLine.findFlag("url", false);
  601.     if (urlFlagIdx > -1 && (osintFlagIdx > -1 ||
  602.         cmdLine.state == nsICommandLine.STATE_REMOTE_EXPLICIT)) {
  603.       var urlParam = cmdLine.getArgument(urlFlagIdx + 1);
  604.       if (cmdLine.length != urlFlagIdx + 2 || /firefoxurl:/.test(urlParam))
  605.         throw NS_ERROR_ABORT;
  606.       cmdLine.handleFlag("osint", false)
  607.     }
  608.   },
  609.  
  610.   /* nsIFactory */
  611.   createInstance: function bch_CI(outer, iid) {
  612.     if (outer != null)
  613.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  614.  
  615.     return this.QueryInterface(iid);
  616.   },
  617.     
  618.   lockFactory : function bch_lock(lock) {
  619.     /* no-op */
  620.   }
  621. };
  622.  
  623. const bch_contractID = "@mozilla.org/browser/clh;1";
  624. const bch_CID = Components.ID("{5d0ce354-df01-421a-83fb-7ead0990c24e}");
  625. const CONTRACTID_PREFIX = "@mozilla.org/uriloader/content-handler;1?type=";
  626.  
  627. function handURIToExistingBrowser(uri, location, cmdLine)
  628. {
  629.   if (!shouldLoadURI(uri))
  630.     return;
  631.  
  632.   var navWin = getMostRecentBrowserWindow();
  633.   if (!navWin) {
  634.     // if we couldn't load it in an existing window, open a new one
  635.     openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  636.                "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  637.                uri.spec);
  638.     return;
  639.   }
  640.  
  641.   var navNav = navWin.QueryInterface(nsIInterfaceRequestor)
  642.                      .getInterface(nsIWebNavigation);
  643.   var rootItem = navNav.QueryInterface(nsIDocShellTreeItem).rootTreeItem;
  644.   var rootWin = rootItem.QueryInterface(nsIInterfaceRequestor)
  645.                         .getInterface(nsIDOMWindow);
  646.   var bwin = rootWin.QueryInterface(nsIDOMChromeWindow).browserDOMWindow;
  647.   bwin.openURI(uri, null, location,
  648.                nsIBrowserDOMWindow.OPEN_EXTERNAL);
  649. }
  650.  
  651.  
  652. var nsDefaultCommandLineHandler = {
  653.   /* nsISupports */
  654.   QueryInterface : function dch_QI(iid) {
  655.     if (!iid.equals(nsISupports) &&
  656.         !iid.equals(nsICommandLineHandler) &&
  657.         !iid.equals(nsIFactory))
  658.       throw Components.results.NS_ERROR_NO_INTERFACE;
  659.  
  660.     return this;
  661.   },
  662.  
  663.   // List of uri's that were passed via the command line without the app
  664.   // running and have already been handled. This is compared against uri's
  665.   // opened using DDE on Win32 so we only open one of the requests.
  666.   _handledURIs: [ ],
  667. //@line 717 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  668.  
  669.   /* nsICommandLineHandler */
  670.   handle : function dch_handle(cmdLine) {
  671.     var urilist = [];
  672.  
  673. //@line 743 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  674.  
  675.     try {
  676.       var ar;
  677.       while ((ar = cmdLine.handleFlagWithParam("url", false))) {
  678.         var found = false;
  679.         var uri = resolveURIInternal(cmdLine, ar);
  680.         // count will never be greater than zero except on Win32.
  681.         var count = this._handledURIs.length;
  682.         for (var i = 0; i < count; ++i) {
  683.           if (this._handledURIs[i].spec == uri.spec) {
  684.             this._handledURIs.splice(i, 1);
  685.             found = true;
  686.             cmdLine.preventDefault = true;
  687.             break;
  688.           }
  689.         }
  690.         if (!found) {
  691.           urilist.push(uri);
  692.           // The requestpending command line flag is only used on Win32.
  693.           if (cmdLine.handleFlag("requestpending", false) &&
  694.               cmdLine.state == nsICommandLine.STATE_INITIAL_LAUNCH)
  695.             this._handledURIs.push(uri)
  696.         }
  697.       }
  698.     }
  699.     catch (e) {
  700.       Components.utils.reportError(e);
  701.     }
  702.  
  703.     count = cmdLine.length;
  704.  
  705.     for (i = 0; i < count; ++i) {
  706.       var curarg = cmdLine.getArgument(i);
  707.       if (curarg.match(/^-/)) {
  708.         Components.utils.reportError("Warning: unrecognized command line flag " + curarg + "\n");
  709.         // To emulate the pre-nsICommandLine behavior, we ignore
  710.         // the argument after an unrecognized flag.
  711.         ++i;
  712.       } else {
  713.         try {
  714.           urilist.push(resolveURIInternal(cmdLine, curarg));
  715.         }
  716.         catch (e) {
  717.           Components.utils.reportError("Error opening URI '" + curarg + "' from the command line: " + e + "\n");
  718.         }
  719.       }
  720.     }
  721.  
  722.     if (urilist.length) {
  723.       if (cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH &&
  724.           urilist.length == 1) {
  725.         // Try to find an existing window and load our URI into the
  726.         // current tab, new tab, or new window as prefs determine.
  727.         try {
  728.           handURIToExistingBrowser(urilist[0], nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, cmdLine);
  729.           return;
  730.         }
  731.         catch (e) {
  732.         }
  733.       }
  734.  
  735.       var URLlist = urilist.filter(shouldLoadURI).map(function (u) u.spec);
  736.       if (URLlist.length) {
  737.         openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  738.                    "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  739.                    URLlist);
  740.       }
  741.  
  742.     }
  743.     else if (!cmdLine.preventDefault) {
  744.       // Passing defaultArgs, so use NO_EXTERNAL_URIS
  745.       openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  746.                  "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  747.                  nsBrowserContentHandler.defaultArgs, NO_EXTERNAL_URIS);
  748.     }
  749.   },
  750.  
  751.   // XXX localize me... how?
  752.   helpInfo : "Usage: firefox [-flags] [<url>]\n",
  753.  
  754.   /* nsIFactory */
  755.   createInstance: function dch_CI(outer, iid) {
  756.     if (outer != null)
  757.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  758.  
  759.     return this.QueryInterface(iid);
  760.   },
  761.     
  762.   lockFactory : function dch_lock(lock) {
  763.     /* no-op */
  764.   }
  765. };
  766.  
  767. const dch_contractID = "@mozilla.org/browser/final-clh;1";
  768. const dch_CID = Components.ID("{47cd0651-b1be-4a0f-b5c4-10e5a573ef71}");
  769.  
  770. var Module = {
  771.   /* nsISupports */
  772.   QueryInterface: function mod_QI(iid) {
  773.     if (iid.equals(Components.interfaces.nsIModule) ||
  774.         iid.equals(Components.interfaces.nsISupports))
  775.       return this;
  776.  
  777.     throw Components.results.NS_ERROR_NO_INTERFACE;
  778.   },
  779.  
  780.   /* nsIModule */
  781.   getClassObject: function mod_getco(compMgr, cid, iid) {
  782.     if (cid.equals(bch_CID))
  783.       return nsBrowserContentHandler.QueryInterface(iid);
  784.  
  785.     if (cid.equals(dch_CID))
  786.       return nsDefaultCommandLineHandler.QueryInterface(iid);
  787.  
  788.     throw Components.results.NS_ERROR_NO_INTERFACE;
  789.   },
  790.     
  791.   registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
  792.     if (Components.classes["@mozilla.org/xre/app-info;1"]) {
  793.       // Don't register these if Firefox is launching a XULRunner application
  794.       const FIREFOX_UID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
  795.       var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
  796.                               .getService(Components.interfaces.nsIXULAppInfo);
  797.       if (appInfo.ID != FIREFOX_UID)
  798.         return;
  799.     }
  800.  
  801.     var compReg =
  802.       compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
  803.  
  804.     compReg.registerFactoryLocation( bch_CID,
  805.                                      "nsBrowserContentHandler",
  806.                                      bch_contractID,
  807.                                      fileSpec,
  808.                                      location,
  809.                                      type );
  810.     compReg.registerFactoryLocation( dch_CID,
  811.                                      "nsDefaultCommandLineHandler",
  812.                                      dch_contractID,
  813.                                      fileSpec,
  814.                                      location,
  815.                                      type );
  816.  
  817.     function registerType(contentType) {
  818.       compReg.registerFactoryLocation( bch_CID,
  819.                                        "Browser Cmdline Handler",
  820.                                        CONTRACTID_PREFIX + contentType,
  821.                                        fileSpec,
  822.                                        location,
  823.                                        type );
  824.     }
  825.  
  826.     registerType("text/html");
  827.     registerType("application/vnd.mozilla.xul+xml");
  828. //@line 898 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  829.     registerType("image/svg+xml");
  830. //@line 900 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/nsBrowserContentHandler.js"
  831.     registerType("text/rdf");
  832.     registerType("text/xml");
  833.     registerType("application/xhtml+xml");
  834.     registerType("text/css");
  835.     registerType("text/plain");
  836.     registerType("image/gif");
  837.     registerType("image/jpeg");
  838.     registerType("image/jpg");
  839.     registerType("image/png");
  840.     registerType("image/bmp");
  841.     registerType("image/x-icon");
  842.     registerType("image/vnd.microsoft.icon");
  843.     registerType("image/x-xbitmap");
  844.     registerType("application/http-index-format");
  845.  
  846.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  847.                            .getService(nsICategoryManager);
  848.  
  849.     catMan.addCategoryEntry("command-line-handler",
  850.                             "m-browser",
  851.                             bch_contractID, true, true);
  852.     catMan.addCategoryEntry("command-line-handler",
  853.                             "x-default",
  854.                             dch_contractID, true, true);
  855.     catMan.addCategoryEntry("command-line-validator",
  856.                             "b-browser",
  857.                             bch_contractID, true, true);
  858.   },
  859.     
  860.   unregisterSelf : function mod_unregself(compMgr, location, type) {
  861.     var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
  862.     compReg.unregisterFactoryLocation(bch_CID, location);
  863.     compReg.unregisterFactoryLocation(dch_CID, location);
  864.  
  865.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  866.                            .getService(nsICategoryManager);
  867.  
  868.     catMan.deleteCategoryEntry("command-line-handler",
  869.                                "m-browser", true);
  870.     catMan.deleteCategoryEntry("command-line-handler",
  871.                                "x-default", true);
  872.     catMan.deleteCategoryEntry("command-line-validator",
  873.                                "b-browser", true);
  874.   },
  875.  
  876.   canUnload: function(compMgr) {
  877.     return true;
  878.   }
  879. };
  880.  
  881. // NSGetModule: Return the nsIModule object.
  882. function NSGetModule(compMgr, fileSpec) {
  883.   return Module;
  884. }
  885.